package data_structur.ArrayQueue;

import java.util.HashMap;
import java.util.Map;

public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(5);
        arrayQueue.add(1999);
        arrayQueue.add(12);
        arrayQueue.add(11);
        arrayQueue.add(7);
        arrayQueue.add(19);
        arrayQueue.add(88);
        arrayQueue.showQueue();
        System.out.println(arrayQueue.showHeadQueue());
        System.out.println(arrayQueue.get());
        System.out.println(arrayQueue.get());
        System.out.println(arrayQueue.get());
        System.out.println(arrayQueue.get());
        System.out.println(arrayQueue.get());
        System.out.println(arrayQueue.get());
        arrayQueue.showQueue();
    }

}

class ArrayQueue{
    private final int maxQueueSize;
    private final int[] arr; // 存储数据，数组模拟队列
    private int front;
    private int rear;

    // 创建队列构造器
    public ArrayQueue(int maxQueueSize){
        this.maxQueueSize = maxQueueSize;
        this.arr = new int[maxQueueSize];
        this.front = -1;
        this.rear = -1;
    }

    // 判断已满
    public boolean isFull(){
        return rear == maxQueueSize - 1;
    }

    // 判断为空
    public boolean isEmpty(){
        return front == rear;
    }

    // 入队列
    public void add(int i){
        if (isFull()){
            System.out.println("add() Full ArrayQueue.");
        } else {
            rear++;
            arr[rear] = i;
        }
    }

    // 出队列
    public int get(){
        if (isEmpty()){
            System.out.println("get() Empty ArrayQueue.");
            return -1;
        } else {
            front++;
            return arr[front];
        }
    }

    // 查看队列
    public void showQueue(){
        if (isEmpty()){
            System.out.println("showQueue() Empty ArrayQueue.");
        } else {
            for (int i : arr) {
                System.out.print(i+" ");
            }
            System.out.println();
        }
    }

    // 查看头部
    public int showHeadQueue(){
        if (isEmpty()){
            System.out.println("showHeadQueue() Empty ArrayQueue.");
            return -1;
        } else {
            return arr[front+1];
        }
    }
}